/*
  ToyCar: ESP32-C3
       by Cheng Maohua
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WebServer.h>

#include "component_drv8833_esp32_simple_motor_ackermann.h"

// #define CONFIG_DEBUG

// Set these to your desired credentials.
const char *ssid = "toycar";
const char *password = "12345678";

WebServer server(80);

const char webpage[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
   html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}
   .button { background-color: #195B6A; border: none; color: white; padding: 3px 10px;
            width:150px; height: 30px;text-decoration: none; font-size: 18px; margin: 2px; cursor: pointer;}
</style>
</head>
<title>ToyCar</title>
<body>
<h1 align=center>ToyCar</h1>
<p><button class="button" onclick="send('FORWARD')">Go</button></p>
<p><button class="button" onclick="send('STOP')">STOP</button></p>
<p><button class="button" onclick="send('BACKWARD')">BACK</button></p>
<p><button class="button" onclick="send('TURN_LEFT')">Turn Left</button></p>
<p><button class="button" onclick="send('CENTER')">Center</button></p>
<p><button class="button" onclick="send('TURN_RIGHT')">Turn Right</button></p>
<p><button class="button" onclick="send('SPEED_UP')">Speed Up</button>&ensp;&ensp;
   <button class="button" onclick="send('SPEED_DOWN')">Speed Down</button></p>
<div><h2> 
  <h2>Robot: <span id="robot_speed_pwm_val">0</span><br></h2>
  <h2>Robot State: <span id="state">NA</span> </h2>
 </div>
<script>
function send(robot_cmd) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("state").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "robot_set?state="+robot_cmd, true);
  xhttp.send();
}

setInterval(function() 
{
  getData();
}, 1000); 

function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var dataobj=JSON.parse(this.responseText);
      document.getElementById("robot_speed_pwm_val").innerHTML = dataobj.robot_speed_pwm;
    }
  };
  xhttp.open("GET", "data_read", true);
  xhttp.send();
}
</script>
</body>
</html>
)=====";

void webclient_cmd(String inString) {
  ROBOT_ACTION_CODE cur_robot_cmd = robot_state;
  if (inString.equalsIgnoreCase("FORWARD")) {
    cur_robot_cmd = ROBOT_FORWARD;
  } else if (inString.equalsIgnoreCase("STOP")) {
    cur_robot_cmd = ROBOT_STOP;
  } else if (inString.equalsIgnoreCase("BACKWARD")) {
    cur_robot_cmd = ROBOT_BACKWARD;
  } else if (inString.equalsIgnoreCase("TURN_LEFT")) {
    robot_action(ROBOT_TURN_LEFT);
  } else if (inString.equalsIgnoreCase("TURN_RIGHT")) {
    robot_action(ROBOT_TURN_RIGHT);
  } else if (inString.equalsIgnoreCase("CENTER")) {
    robot_action(ROBOT_CENTER);
  }  else if (inString.equalsIgnoreCase("SPEED_UP")) {
    cur_robot_cmd = ROBOT_SPEED_UP;
  } else if (inString.equalsIgnoreCase("SPEED_DOWN")) {
    cur_robot_cmd = ROBOT_SPEED_DOWN;
  }
  Serial.print("robot cmd: ");
  Serial.println(cur_robot_cmd);
  if (cur_robot_cmd != robot_state) {
    robot_action(cur_robot_cmd);
  };
}

void handleRoot() {
  String s = webpage;
  server.send(200, "text/html", s);
}

void handleRobot() {
  String robot_cmd = server.arg("state");
  Serial.println(robot_cmd);
  server.send(200, "text/plane", robot_cmd);
  webclient_cmd(robot_cmd);
}

void handleSensor() {
  if (data_ready) {
    server.send(200, "text/json", json);
  } else {
    server.send(503, "text/plane", "none data");
  }
}

void setup_web_server() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("Configuring access point...");
  // You can remove the password parameter if you want the AP to be open.
  // a valid password must have more than 7 characters
  if (!WiFi.softAP(ssid, password)) {
    log_e("Soft AP creation failed.");
    while (1)
      ;
  }

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.on("/", handleRoot);
  server.on("/robot_set", handleRobot);
  server.on("/data_read", handleSensor);
  server.begin();
  Serial.println("Server started");
}

void loop_web_server() {
  server.handleClient();
}
